// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

/**
 * 我的银行卡界面
 */
cc.Class({
    extends: cc.Component,

    properties: {
        userBindBankPop: cc.Prefab,  //绑定银行卡预制
        ndBankContent: cc.Node, 
        addCard:cc.Node,  //添加银行卡
        bankItem: cc.Prefab,  //银行卡item
        bankcardUnbindPop: cc.Prefab,  //我的银行卡
        callback: null,
        _type:0, //1:表示提现界面跳转过来2:表示银行卡管理跳转过来
        _currentPage:0,
        isNowRefreshing:false,
        callback:null,
        maxBankCardNum:0,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this._currentPage = 1;
        this.isNowRefreshing = false;
        this.loadBankCard(this._currentPage);
    },

    init: function(data,callback){
        this._type = data;
        this.callback = callback;     
    },

    onAddBankCard: function(){
        if(this.maxBankCardNum>0)
        {
            ComponentsUtils.showTips("最多只能绑定1张银行卡");           
        }
        else
        {
            this.onGotoBindCard();
        }
    },

    onGotoBindCard: function(){
        var canvas = cc.find("Canvas");
        var userBindBank = cc.instantiate(this.userBindBankPop);
        var Callback = function(data){
                this.resetLoadBankCard();
            }.bind(this);
        userBindBank.getComponent("user_bindBank_pop").init(Callback);
        canvas.addChild(userBindBank);
        
    },

    //重新加载列表
    resetLoadBankCard:function(){
        this.ndBankContent.removeAllChildren();
        this.maxBankCardNum = 0;
        this._currentPage = 1;
        this.loadBankCard(1);
    },

    loadBankCard:function(page){
        var recv = function(ret){
            ComponentsUtils.unblock();
            if(ret.Code != 0 && ret.Code != 49)
            {
                ComponentsUtils.showTips(ret.Msg);
                cc.error(ret.Msg);
            }
            else
            {
                if(ret.Data != null)
                {
                    for(var i=0;i<ret.Data.length;i++)
                    {
                        var lastnum = ret.Data[i].CardNum.substr(ret.Data[i].CardNum.length-4);
                        var item = cc.instantiate(this.bankItem);
                        item.getComponent("user_bankCard_item").init({
                            bankName:ret.Data[i].BankName,
                            lastNum:lastnum,
                        });
                        var clickEventHandler = new cc.Component.EventHandler();
                        clickEventHandler.target = this.node; 
                        clickEventHandler.component = "user_bankCard_pop"
                        clickEventHandler.handler = "onClickCallBack";
                        clickEventHandler.customEventData = {bankcode:ret.Data[i].BankCode,last:lastnum,bankname:ret.Data[i].BankName};
                        item.getComponent(cc.Button).clickEvents.push(clickEventHandler);
                        this.ndBankContent.addChild(item);
                        this.maxBankCardNum++;
                    }
                    this._currentPage++;
                    this.addCard.active = false;
                }
                else
                {
                    this.addCard.active = true;
                }
            }
        }.bind(this);          

        var data = {
            Token:User.getLoginToken(),
            UserCode:User.getUserCode(),
            PageNumber:page,
            RowsPerPage:11,
        }
        CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.GETBANKCARDLIST, data, recv.bind(this),"POST");     
        ComponentsUtils.block();
    },

    onClickCallBack: function(event, customEventData){
        if(this._type == 1)//选择银行卡
        {
            this.callback(customEventData);
            this.node.getComponent("Page").backAndRemove();
        }
        else if(this._type == 2)//解除绑定
        {
            var canvas = cc.find("Canvas");
            var bankcardUnbind = cc.instantiate(this.bankcardUnbindPop);
            var Callback = function(data){  //解绑银行卡回调
                this.resetLoadBankCard();
            }.bind(this);
            bankcardUnbind.getComponent("user_bankcardUnbind_pop").init(customEventData,Callback);
            canvas.addChild(bankcardUnbind);

        }
    },

    onClose: function(){
        if(this.callback)
        this.callback(null);
        this.node.getComponent("Page").backAndRemove();
    },

    // update (dt) {},
});
